package com.updesignsz.admin.mapper;

import com.updesignsz.commom.entity.Banner;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 横幅图片
 * Creation date 2020-8-8 15:02:24
 */
@Repository
@Mapper
public interface BannerMapper {

    @Select({"<script>",
            "select a.id, a.project_id as projectId, b.name, b.cover_thumbnail as thumbnail, a.created_date as createdDate, a.sequence ",
            "from banner a ",
            "inner join projects b on a.project_id = b.id",
            "<where>",
            "<if test=\"type != null\" >AND a.type = #{type}</if>",
            "</where>",
            "order by  a.sequence desc, a.created_date desc",
            "</script>"
    })
    List<Map<String, Object>> listAll(Banner type);


    @Select("select * from banner where id = #{id}")
    Banner getById(@Param("id") Integer id);

    @Insert({
            "insert into banner(`type`, project_id, sequence, is_removed, created_date, updated_date) ",
            "values (#{type},#{projectId},1,0,NOW(),NOW())"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveBanner(Banner type);


    @Update({"<script>",
            "update banner <set>",
            "<if test=\"type != null\" >type = #{type},</if>",
            "<if test=\"image != null\" >image = #{image},</if>",
            "<if test=\"imageThumbnail != null\" >image_thumbnail = #{imageThumbnail},</if>",
            "<if test=\"projectId != null\" >project_id = #{projectId},</if>",
            "<if test=\"sequence != null\" >sequence = #{sequence},</if>",
            "updated_date = NOW()",
            "</set> where id = #{id}",
            "</script>"
    })
    void updateBanner(Banner type);

    @Delete("delete from banner where id = #{id}")
    void deleteBanner(@Param("id") Integer id);

}